package com.zoo.common;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.zoo.entity.Product;
import com.zoo.service.ProductBiz;

@Component
public class CarBean {	
	private Product product ;
	private Integer num = 0 ;
	@Resource
	private ProductBiz biz ;
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public Integer getNum() {
		return num;
	}
	public void setNum(Integer num) {
		this.num = num;
	}
	
	public Double getCount() {
		return product==null? 0.0 : product.getPrice()*num;
	}
	public Map<String,CarBean> addToCar(String productid,Map<String,CarBean> car){
		//判断购物车是否存该商品
		boolean flag = true ;
		if(car==null){			
			car = new HashMap<String,CarBean>();
			CarBean bean = new CarBean();
			bean.setNum(1);
			bean.setProduct(biz.findByID(Integer.parseInt(productid)));
			car.put(productid, bean);
			flag = false ;
		}else{
			for(String key : car.keySet()){
				CarBean bean = car.get(key);
				if(bean.getProduct().getId()==Integer.parseInt(productid)){
					bean.setNum(bean.getNum()+1);
					flag = false ;
				}
			}
		}
		if(flag){
			CarBean bean = new CarBean();
			bean.setNum(1);
			bean.setProduct(biz.findByID(Integer.parseInt(productid)));
			car.put(productid, bean);
		}
		return car ;
	}

}
